/* @author Miachael Antemann 
 * updated by Shanmugaesan Shanmugaratnam
 *  @created on 07.12.2008 15:13:56
 */
package de.fhhannover.inform.wari.grp8.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import de.fhhannover.inform.wari.grp8.spiel.StrategyHandler;

@SuppressWarnings("serial")
public class StrategyGui extends JFrame implements ActionListener {
	/**
	 * @param args
	 */
	private Gui_Controller mgCon;
	private Color mColor;
	private JTable jTab;
	private JButton jBtn;
	private Container con;
	private String grpName_Arr[];// = new String[50];
	private String grpID_Arr[];// = new String[50];
	private String path_Arr[];// = new String[50];
	// String data[][] = new String[14][14];
	private int indexCount = 30;
	String mKiType;
	boolean isKiA, isKiB;
	private JScrollPane pane;

	StrategyGui(String kiType, Gui_Controller guiCon) {
		// TODO Auto-generated method stub
		this.mKiType = kiType;
		this.mgCon = guiCon;
		con = this.getContentPane();
		con.setLayout(new BorderLayout());
		jBtn = new JButton("Ok");
		con.setBackground(mColor);
		int i = 0;
		Vector<String> vecId = StrategyHandler.getStrategyHandler()
				.getStrategyIDs();
		Vector<String> vecName = StrategyHandler.getStrategyHandler()
				.getStrategyNames();
		Vector<String> vecPath = StrategyHandler.getStrategyHandler()
				.getStrategyPaths();
		Enumeration<String> elId = vecId.elements();
		Enumeration<String> elName = vecName.elements();
		Enumeration<String> elPath = vecPath.elements();
		// einamal durchzählen
		while (elId.hasMoreElements()) {
			elId.nextElement();
			// System.out.println("ID "+grpID_Arr[i]);
			i++;
		}
		vecId = StrategyHandler.getStrategyHandler().getStrategyIDs();
		elId = vecId.elements();
		indexCount = i;
		grpName_Arr = new String[i + 1];
		grpID_Arr = new String[i + 1];
		path_Arr = new String[i + 1];
		for (int x = 0; x < i; x++) {
			grpID_Arr[x] = elId.nextElement();
			grpName_Arr[x] = elName.nextElement();
			path_Arr[x] = elPath.nextElement();
		}

		String[] columnNames = { "Index", "Gruppen ID", "Ki Name", "Pfad" };
		Object[][] data = new Object[indexCount][4];

		for (int a = 0; a < i; a++) {

			// System.out.println(""+grpID_Arr[a]+" | "+grpName_Arr[a]);
			data[a][0] = Integer.toString(a + 1);
			data[a][1] = grpID_Arr[a];
			data[a][2] = grpName_Arr[a];
			data[a][3] = path_Arr[a];
		}
		jTab = new JTable(data, columnNames);
		jTab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn col;
		col = jTab.getColumn("Index");
		col.setPreferredWidth(45);
		col = jTab.getColumn("Gruppen ID");
		col.setPreferredWidth(120);
		col = jTab.getColumn("Ki Name");
		col.setPreferredWidth(150);
		col = jTab.getColumn("Pfad");
		col.setPreferredWidth(500);
		pane = new JScrollPane(jTab);
		pane.setVerticalScrollBar(new JScrollBar());
		// con.add(jTab.getTableHeader(),BorderLayout.PAGE_START);
		// con.add(jTab,BorderLayout.CENTER);
		con.add(pane, BorderLayout.CENTER);
		// con.add(jTab.getTableHeader(), BorderLayout.PAGE_START);
		// con.add(jTab, BorderLayout.CENTER);
		if (mKiType.equalsIgnoreCase("A")) {
			setTitle("Ki Chooser A");
		}
		if (mKiType.equalsIgnoreCase("B")) {
			setTitle("Ki Chooser B");
		}
		jBtn.addActionListener(this);
		con.add(jBtn, BorderLayout.SOUTH);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setSize(830, 300);
		this.setLocation(280, 10);
	}

	public void actionPerformed(ActionEvent arg) {
		String command = arg.getActionCommand();
		if (command.equals("Ok")) {
			int rowTab = jTab.getSelectedRow();
			// int col = jTab.getSelectedColumn();
			// System.out.println("Row "+rowTab);
			if (rowTab != -1) {
				if (mKiType.equalsIgnoreCase("A")) {
					// System.out.println("geklickte Zeile A : "
					// + grpID_Arr[rowTab] + " " + grpName_Arr[rowTab]);
					mgCon.setKIPreferences(true, rowTab);
				} else if (mKiType.equalsIgnoreCase("B")) {
					// System.out.println("geklickte Zeile B : "
					// + grpID_Arr[rowTab] + " " + grpName_Arr[rowTab]);
					mgCon.setKIPreferences(false, rowTab);
				}
				this.dispose();
			}
		}
	}

	// public static void main(String args[]){
	// StrategyGui stguiA = new StrategyGui("B",null);
	// stguiA.setVisible(true);
	// }
}
